package com.rightsidetech.http.controller.admin.system;

import com.github.pagehelper.PageInfo;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.system.dtos.request.SysStoreIdReq;
import com.rightsidetech.system.dtos.request.SysStoreStorkPageReq;
import com.rightsidetech.system.dtos.request.SysStoreStorkReplenishInfoReq;
import com.rightsidetech.system.dtos.request.SysStoreStorkReplenishReq;
import com.rightsidetech.system.dtos.response.SysStoreStorkInfoResp;
import com.rightsidetech.system.dtos.response.SysStoreStorkPageResp;
import com.rightsidetech.system.dtos.response.SysStoreStorkReplenishInfoResp;
import com.rightsidetech.system.dtos.response.SysStoreStorkReplenishResp;
import com.rightsidetech.system.entity.SysUserLoginInfo;
import com.rightsidetech.system.service.SysStoreStorkService;
import com.rightsidetech.system.service.SysUserLoginInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;

/**
 * @Author: yhr
 * @Date: 2021-12-10 14:00
 */
@RestController
@RequestMapping("/admin/store_stork")
@ShenyuSpringMvcClient(path = "/admin/store_stork/**")
@Api(tags = "admin:门店库存")
public class SysStoreStorkController extends BaseController {
    @Resource
    private SysStoreStorkService sysStoreStorkService;
    @Resource
    private SysUserLoginInfoService sysUserLoginInfoService;

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/page")
    @ApiOperation("库存列表")
    public ResponseData<PageInfo<SysStoreStorkPageResp>> page(SysStoreStorkPageReq page) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(page);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }

        SysUserLoginInfo loginInfo = sysUserLoginInfoService.selectByLoginInfo(Long.parseLong(getAttribute()));
        if (ObjectUtils.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.NOLOGINACCOUNT);
        }
        if(loginInfo.getSysCompanyAreaId() != 0){
            page.setAreaId(loginInfo.getSysCompanyAreaId());
        }

        return sysStoreStorkService.page(page);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/storkInfo")
    @ApiOperation("库存详情列表")
    public ResponseData<PageInfo<SysStoreStorkInfoResp>> storkInfo(SysStoreIdReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        SysUserLoginInfo loginInfo = sysUserLoginInfoService.selectByLoginInfo(Long.parseLong(getAttribute()));
        if (ObjectUtils.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.NOLOGINACCOUNT);
        }
        if(loginInfo.getSysCompanyAreaId() != 0){
            req.setAreaId(loginInfo.getSysCompanyAreaId());
        }
        return sysStoreStorkService.selectReplenishInfo(req);
    }


    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/replenish")
    @ApiOperation("补货记录列表")
    public ResponseData<PageInfo<SysStoreStorkReplenishResp>> replenish(SysStoreStorkReplenishReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }

        SysUserLoginInfo loginInfo = sysUserLoginInfoService.selectByLoginInfo(Long.parseLong(getAttribute()));
        if (ObjectUtils.isEmpty(loginInfo)){
            return ResponseData.fail(CodeIdEnum.NOLOGINACCOUNT);
        }
        if(loginInfo.getSysCompanyAreaId() != 0){
            req.setAreaId(loginInfo.getSysCompanyAreaId().toString());
        }

        return sysStoreStorkService.replenishPage(req);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/replenishInfo")
    @ApiOperation("补货详情列表")
    public ResponseData<PageInfo<SysStoreStorkReplenishInfoResp>> replenishInfo(SysStoreStorkReplenishInfoReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        return sysStoreStorkService.replenishInfo(req);
    }
}
